import * as React from "react"
import { Metadata } from "next"
import { type SearchParams } from "@/types/table"
import { getValidFilters } from "@/lib/data-table"
import { Shell } from "@/components/shell"
import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton"
import { searchParamsRFQDashboardCache } from "@/lib/b-rfq/validations"
import { getRFQDashboard } from "@/lib/b-rfq/service"
import { RFQDashboardTable } from "@/lib/b-rfq/summary-table/summary-rfq-table"
import { InformationButton } from "@/components/information/information-button"

export const metadata: Metadata = {
  title: "견적 RFQ",
  description: "",
}

interface PQReviewPageProps {
  searchParams: Promise<SearchParams>
}

export default async function PQReviewPage(props: PQReviewPageProps) {
  const searchParams = await props.searchParams
  const search = searchParamsRFQDashboardCache.parse(searchParams)
  const validFilters = getValidFilters(search.filters)

  // 기본 필터 처리 (통일된 이름 사용)
  let basicFilters = []
  if (search.basicFilters && search.basicFilters.length > 0) {
    basicFilters = search.basicFilters
    console.log("Using search.basicFilters:", basicFilters);
  } else {
    console.log("No basic filters found");
  }

  // 모든 필터를 합쳐서 처리
  const allFilters = [...validFilters, ...basicFilters]
  
  // 조인 연산자도 통일된 이름 사용
  const joinOperator = search.basicJoinOperator || search.joinOperator || 'and';

  // Promise.all로 감싸서 전달
  const promises = Promise.all([
    getRFQDashboard({
      ...search,
      filters: allFilters,
      joinOperator,
    })
  ])

  console.log(search, "견적")

  return (
    <Shell className="gap-4">
      <div className="flex items-center justify-between space-y-2">
        <div className="flex items-center justify-between space-y-2">
          <div>
            <div className="flex items-center gap-2">
              <h2 className="text-2xl font-bold tracking-tight">
                견적 RFQ
              </h2>
              <InformationButton pagePath="evcp/b-rfq" />
            </div>
          </div>
        </div>
      </div>
      
      {/* Items처럼 직접 테이블 렌더링 */}
      <React.Suspense 
        fallback={
          <DataTableSkeleton
            columnCount={8}
            searchableColumnCount={2}
            filterableColumnCount={3}
            cellWidths={["10rem", "15rem", "12rem", "12rem", "8rem", "8rem", "10rem", "8rem"]}
            shrinkZero
          />
        }
      >
        <RFQDashboardTable promises={promises} />
      </React.Suspense>
    </Shell>
  )
}